﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data;
using System.Data.Common;
using System.Data.Entity.Infrastructure;

namespace eGrocery.BaseDAL.Model
{
    partial class eGroceryEntities
    {
        public override int SaveChanges()
        {
            ChangeTracker.DetectChanges(); // Important!

            ObjectContext ctx = ((IObjectContextAdapter)this).ObjectContext;

            List<ObjectStateEntry> objectStateEntryList =
                ctx.ObjectStateManager.GetObjectStateEntries(EntityState.Added
                                                           | EntityState.Modified
                                                           | EntityState.Deleted)
                .ToList();

            foreach (ObjectStateEntry entry in objectStateEntryList)
            {
                if (Convert.ToString(entry.Entity) == "ChangeHistory")
                    continue;
                if (!entry.IsRelationship)
                {
                    switch (entry.State)
                    {
                        case EntityState.Added:
                            // write log...
                            break;
                        case EntityState.Deleted:
                            // write log...
                            break;
                        case EntityState.Modified:
                            {
                                foreach (string propertyName in
                                             entry.GetModifiedProperties())
                                {
                                    DbDataRecord original = entry.OriginalValues;
                                    string oldValue = original.GetValue(
                                        original.GetOrdinal(propertyName))
                                        .ToString();

                                    CurrentValueRecord current = entry.CurrentValues;
                                    string newValue = current.GetValue(
                                        current.GetOrdinal(propertyName))
                                        .ToString();

                                    if (oldValue != newValue) // probably not necessary
                                    {
                                        //Log.WriteAudit(
                                        //    "Entry: {0} Original :{1} New: {2}",
                                        //    entry.Entity.GetType().Name,
                                        //    oldValue, newValue);
                                        Global.Context.ChangeHistories.Add(
                                            new ChangeHistory
                                            {
                                                TableName = Convert.ToString(entry.Entity),
                                                ColumnName = propertyName,
                                                PreviousValue = oldValue,
                                                NewValue = newValue,
                                                ModificationDate = DateTime.UtcNow,
                                                Modifiedby = Global.CurrentSessionUser
                                            }
                                        );
                                        
                                    }
                                }
                                break;
                            }
                    }
                }
            }
            return base.SaveChanges();
        }

    }
}
